<?php

namespace App\Library\Traits\Orm;

use App\Models\AuctionBailModel;
use App\Models\AuctionBidModel;
use App\Models\AuctionCertificationModel;
use App\Models\AuctionFollowModel;
use App\Models\MediaRelationModel;
use App\Models\ShopUserAddressModel;
use App\Models\ShopUserModel;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

trait AuctionOrmTrait
{
    public function media(): HasMany
    {
        return $this->hasMany(MediaRelationModel::class, 'object_id')->where('object_type', 1)
            ->whereNull('deleted_at');
    }

    public function bail(): HasOne
    {
        return $this->hasOne(AuctionBailModel::class, 'auction_id', 'id')
            ->whereNull('refund_at')
            ->where([
                'user_id' => userId(),
                'status_pay' => 1
            ]);
    }

    public function follow(): HasOne
    {
        return $this->hasOne(AuctionFollowModel::class, 'auction_id', 'id')
            ->where('user_id', userId());
    }

    public function address(): BelongsTo
    {
        return $this->BelongsTo(ShopUserAddressModel::class, 'publish_id', 'user_id')
            ->where('ua_is_refund', 1);
    }

    public function bid(): BelongsTo
    {
        return self::BelongsTo(AuctionBidModel::class, 'id', 'auction_id')->orderByDesc('id');
    }

    public function publisher(): BelongsTo
    {
        return self::BelongsTo(ShopUserModel::class, 'publish_id', 'user_id');
    }

    public function storeInfo(): BelongsTo
    {
        return self::belongsTo(AuctionCertificationModel::class, 'publish_id', 'user_id');
    }
}
